package com.kdgc.power.common.util;


import com.nlf.calendar.Lunar;
import com.nlf.calendar.LunarMonth;
import com.nlf.calendar.Solar;

import java.time.LocalDate;


public class YearUtil {
    public static int lengthOfLunarMonth(int lunarYear, int lunarMonth, boolean isLeap) {
        LunarMonth month = LunarMonth.fromYm(lunarYear, lunarMonth);
        if (!isLeap) {
            return month.getDayCount();
        } else {
            if (month.next(1).isLeap()) {
                return month.next(1).getDayCount();
            } else {
                return month.getDayCount();
            }
        }
    }

    public static void getTermByName(int year, String termName) {
        for (int i = 0; i < 366; i++) {
            LocalDate localDate = LocalDate.of(year, 2, 1).plusDays(i);
            Lunar lunar = new Lunar(new Solar(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()));
            if (termName.equals(lunar.getJieQi())) {
                LocalDateUtil.getDay(lunar);
                break;
            }
        }
    }

    public static void getTermInYear(int year) {
        for (int i = 0; i < 366; i++) {
            LocalDate localDate = LocalDate.of(year, 2, 1).plusDays(i);
            Lunar lunar = new Lunar(new Solar(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()));
            if (StringUtil.isNotEmpty(lunar.getJieQi())) {
                LocalDateUtil.getDay(lunar);
            }
        }
    }

    public static void printMonth(int year, int month) {
        LocalDate localDate = LocalDate.of(year, month, 1);
        int value = localDate.withDayOfMonth(1).getDayOfWeek().getValue() % 7;
        int max = localDate.lengthOfMonth();
        System.out.printf("%30s", year + "-" + String.format("%02d", month) + "\n\n");
        System.out.println(" 日      一      二      三      四      五      六\n");
        for (int i = 1 - value; i <= max + value; i++) {
            if (i - value <= 0) {
                System.out.printf("%-4s", "");
            } else {
                System.out.printf("%-8s", " " + (i - value));
                if (i % 7 == 0) {
                    System.out.println();
                    for (int j = i - 6; j <= i; j++) {
                        if (j - value <= 0) {
                            System.out.printf("%8s", "");
                        } else {
                            Lunar lunar = new Lunar(new Solar(localDate.getYear(), localDate.getMonthValue(), j - value));
                            String detail = lunar.getDayInChinese();
                            if ("初一".equals(detail)) {
                                detail = lunar.getMonthInChinese();
                                if (!detail.startsWith("闰")) {
                                    detail += "月";
                                }
                            }
                            detail = StringUtil.nvl(lunar.getJieQi(), detail);
                            System.out.printf("%-6s", detail);
                        }
                    }
                    System.out.println();
                }
            }
        }
        System.out.println();
        for (int i = max - (max + value) % 7 + 1; i <= max; i++) {
            Lunar lunar = new Lunar(new Solar(localDate.getYear(), localDate.getMonthValue(), i));
            String detail = lunar.getDayInChinese();
            if ("初一".equals(detail)) {
                detail = lunar.getMonthInChinese();
                if (!detail.startsWith("闰")) {
                    detail += "月";
                }
            }
            detail = StringUtil.nvl(lunar.getJieQi(), detail);
            System.out.printf("%-6s", detail);
        }
    }
}
